package com.ccloud.modules.motorfactory.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccloud.api.motorfactory.domain.MMainEnginePlants;
import com.ccloud.api.motorfactory.domain.MMotorPart;
import com.ccloud.api.motorfactory.domain.MMainEnginePlants;
import com.ccloud.api.motorfactory.service.IMMainEnginePlantsService;
import com.ccloud.api.motorfactory.service.IMMainEnginePlantsService;
import com.ccloud.common.core.reqres.response.Result;
import com.ccloud.common.core.utils.ValueUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Author chentl
 * @Version V1.0
 * @Project wh-bigdata-frame.
 * @Title MainEngineController.java
 * @Description 主机厂项目 控制器
 * @Package com.ccloud.api.motorfactory.controller
 * @Date 2022/4/9 17:52
 * @Copyright: 武汉大数据产业发展有限公司 All rights reserved.
 */
@Slf4j
@RestController
@RequestMapping("/main-engine")
public class MainEngineController {

    @Resource
    private IMMainEnginePlantsService mainEnginePlantsService;


    @GetMapping("/list")
    public Result<IPage<MMainEnginePlants>> list(MMainEnginePlants mainEnginePlants,
                                                 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Result<IPage<MMainEnginePlants>> result = Result.ok();
        Page<MMainEnginePlants> page = new Page<>(pageNo, pageSize);

        IPage<MMainEnginePlants> pageList = mainEnginePlantsService.selectPage(page, mainEnginePlants);

        result.setResult(pageList);
        return result;
    }

    @PostMapping("/add")
    public Result<?> add(@RequestBody MMainEnginePlants mainEnginePlants) {
        Result<MMotorPart> result = new Result<>();
        //查询出最大的id + 1保存
        Integer maxVal = mainEnginePlantsService.maxVal();
        if(Objects.isNull(maxVal)){
            //不存在则从1 开始
            maxVal = 1;
        }else {
            maxVal = maxVal + 1;
        }
        mainEnginePlants.setMainEngineNo(ValueUtils.parseString(maxVal) );
        
        mainEnginePlantsService.save(mainEnginePlants);

        return result;
    }

    @PutMapping("/update")
    public Result<MMainEnginePlants> update(@RequestBody MMainEnginePlants mainEnginePlants) {
        Result<MMainEnginePlants> result = new Result<>();
        mainEnginePlantsService.updateById(mainEnginePlants);
        return result;
    }

    @DeleteMapping("/delete")
    public Result<?> delete(@RequestParam("id") String id) {
        mainEnginePlantsService.removeById(id);
        return Result.ok();
    }


}
